package cn.edu.cjw.APMFP.ui.panel;


import cn.edu.cjw.APMFP.Resource.Resource;
import cn.edu.cjw.APMFP.Resource.Service.ResourceService;
import cn.edu.cjw.APMFP.Resource.Service.ResourceServiceImpl;
import cn.edu.cjw.APMFP.ui.CenterPanel;
import cn.edu.cjw.APMFP.ui.dialog.Bom2Dialog;
import cn.edu.cjw.APMFP.ui.table.Bom2TableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @package cn.edu.cjw.APMFP.ui.panel
 * @className Bom2Panel
 * @note 原料
 * @author wind
 * @date 2022/3/12 19:28
 */
public class Bom2Panel extends JPanel {


    private JButton addBtn = new JButton("入库");

//    private BTService service = new BTServiceImpl();

    private ResourceService service = new ResourceServiceImpl();

    private JPanel northPanel = new JPanel();

    private JScrollPane centerPanel = new JScrollPane();

    private JTable table = new JTable();

    private List<Resource> list = new ArrayList<>();

    private CenterPanel mainPanel;

    public Bom2Panel(CenterPanel mainPanel){
        this.mainPanel = mainPanel;
        init();
    }

    private void init(){
        this.setLayout(new BorderLayout());


        northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        northPanel.add(addBtn);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);
        initTable();
        centerPanel.setViewportView(table);


        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Bom2Dialog(null, () -> {
                    initTable();
                });
            }
        });

        this.add(northPanel, BorderLayout.NORTH);
        this.add(centerPanel, BorderLayout.CENTER);
    }

    public void initTable(){
        this.list.clear();
        try {
            this.list.addAll(service.showMaterial());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Bom2TableModel model = new Bom2TableModel(list);
        table.setModel(model);
        model.fireTableDataChanged();
    }
}
